//
//  MXAddEquipmentController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/3.
//

#import "MXAddEquipmentController.h"
#import "MxPublicNav.h"
#import "MXAddEquipmentView.h"
#import "MXAddStepController.h"
#import "MXAddEquipmentTypeCell.h"
#import "MXEquipmentTypeModel.h"
#import "MXAddRenameController.h"

@interface MXAddEquipmentController () <MxNavigationViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, strong) UIView *animateV;
@property (nonatomic, strong) UILabel *noticeLab;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) MXAddEquipmentView *searchV;
@property (nonatomic,strong) UICollectionView *listCV;
@property (nonatomic,strong) NSMutableArray *listArray;
@end

@implementation MXAddEquipmentController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.listArray = [[NSMutableArray alloc] init];
    [self createUI];
    [self getDeviceTypeList];
}

- (void)createUI {
    
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text = @"添加设备";
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    
    self.animateV = [[UIView alloc] init];
    self.animateV.backgroundColor = [UIColor colorWithHexString:@"#3378F7"];
    [self.view addSubview:self.animateV];
    [self.animateV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(28);
        make.left.offset(16);
        make.top.mas_equalTo(self.nav.mas_bottom).offset(26);
    }];
    
    self.noticeLab = [[UILabel alloc] init];
    [self.view addSubview:self.noticeLab];
    self.noticeLab.text = @"正在搜索附近的设备...";
    self.noticeLab.textColor = [UIColor dc_colorWithHexString:@"#000000" alpha:0.5];
    self.noticeLab.font = PFRFont(13);
    [self.noticeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.animateV.mas_right).offset(11);
        make.centerY.mas_equalTo(self.animateV);
    }];
    
    self.countLab = [[UILabel alloc] init];
    self.countLab.text = @"1";
    self.countLab.textColor = [UIColor colorWithHexString:@"#3378F7"];
    self.countLab.font = PFRFontMedium(17);
    self.countLab.textAlignment = NSTextAlignmentCenter;
    self.countLab.backgroundColor = [UIColor dc_colorWithHexString:@"#3378F7" alpha:0.2];
    self.countLab.layer.cornerRadius = 11;
    self.countLab.layer.masksToBounds = YES;
    [self.view addSubview:self.countLab];
    [self.countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-18);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(43);
        make.centerY.mas_equalTo(self.animateV);
    }];
    
    CGFloat width = (kScreenW - 47) / 2.0;
    self.searchV = [[MXAddEquipmentView alloc] init];
    [self.view addSubview:self.searchV];
    [self.searchV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(116);
        make.left.offset(20);
        make.top.mas_equalTo(self.countLab.mas_bottom).offset(46);
    }];
    
    UILabel *handAddLab = [[UILabel alloc] init];
    handAddLab.backgroundColor = [UIColor colorWithHexString:@"#3378F7"];
    handAddLab.text = @"手动添加";
    handAddLab.layer.cornerRadius = 12;
    handAddLab.layer.masksToBounds = YES;
    handAddLab.textAlignment = NSTextAlignmentCenter;
    handAddLab.textColor = [UIColor whiteColor];
    handAddLab.font = PFRFontMedium(14);
    [self.view addSubview:handAddLab];
    [handAddLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.top.mas_equalTo(self.searchV.mas_bottom).offset(59);
        make.height.mas_equalTo(24);
        make.width.mas_equalTo(79);
    }];
    
    UILabel *tipLab = [[UILabel alloc] init];
    [self.view addSubview:tipLab];
    tipLab.text = @"喂食器";
    tipLab.font = PFRFontMedium(13);
    tipLab.textColor = [UIColor dc_colorWithHexString:@"#000000" alpha:0.5];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(25);
        make.top.mas_equalTo(handAddLab.mas_bottom).offset(8);
    }];
    
    [self.view addSubview:self.listCV];
    [self.listCV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.right.offset(-20);
        make.top.mas_equalTo(tipLab.mas_bottom).offset(18);
        make.bottom.offset(0);
    }];
//    MXAddEquipmentView *standardV = [[MXAddEquipmentView alloc] init];
//    [standardV reloadViewWithType:@"标准版"];
//    MXAddEquipmentView *lookV = [[MXAddEquipmentView alloc] init];
//    
//    [self.view addSubview:standardV];
//    [self.view addSubview:lookV];
//    
//    [standardV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(width);
//        make.height.mas_equalTo(116);
//        make.left.offset(20);
//        make.top.mas_equalTo(tipLab.mas_bottom).offset(18);
//    }];
//    [lookV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(width);
//        make.height.mas_equalTo(116);
//        make.right.offset(-20);
//        make.top.mas_equalTo(tipLab.mas_bottom).offset(18);
//    }];
//    
//    MJWeakSelf;
//    [standardV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
//        [weakSelf addEquipmentWithType:0];
//    }]];
//    
//    [lookV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
//        [weakSelf addEquipmentWithType:1];
//    }]];
//    
}

- (UICollectionView *)listCV {
   if (!_listCV) {
       UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
       layout.itemSize = CGSizeMake((kScreenW - 47) / 2.0, 116);
       layout.minimumLineSpacing = 7;//纵向间距(参考scrollDirection)
       layout.minimumInteritemSpacing = 7;//横向间距
       layout.scrollDirection = UICollectionViewScrollDirectionVertical;
       _listCV = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
       _listCV.backgroundColor = [UIColor clearColor];
       _listCV.delegate = self;
       _listCV.dataSource = self;
       _listCV.showsVerticalScrollIndicator = NO;
       _listCV.showsHorizontalScrollIndicator = NO;
       [_listCV registerClass:[MXAddEquipmentTypeCell class] forCellWithReuseIdentifier:@"MXAddEquipmentTypeCell"];
       
       }
    return _listCV;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.listArray.count;
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MXEquipmentTypeModel *model = self.listArray[indexPath.item];
    MXAddEquipmentTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MXAddEquipmentTypeCell" forIndexPath:indexPath];
    [cell reloadCellWithModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MXEquipmentTypeModel *model = self.listArray[indexPath.item];
    MXAddStepController *stepVc = [[MXAddStepController alloc] init];
    stepVc.categoryId = model.ID;
    stepVc.familyId = self.familyId;
    [self.navigationController pushViewController:stepVc animated:YES];
    
}


- (void)getDeviceTypeList {

     MJWeakSelf
     [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/app/appDeviceCategory/list" params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
         NSDictionary * dict = [responseObject mj_JSONObject];
         [weakSelf.view hideToastActivity];
         if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
             NSArray *dataArray =  [NSArray modelArrayWithClass:[MXEquipmentTypeModel class] json:dict[@"data"]];
             [weakSelf.listArray addObjectsFromArray:dataArray];
             [weakSelf.listCV reloadData];
         } else {

             [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
         }
     } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
         [weakSelf.view hideToastActivity];
     }];
}

- (void)addEquipmentWithType:(NSInteger)type {

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dc_navBarHidden:YES];
}

- (void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
